package com.sanq.product.mcgs.controller;

import org.springframework.web.bind.annotation.PathVariable;

import com.sanq.product.mcgs.entity.vo.LogsVo;
import com.sanq.product.mcgs.service.LogsService;

import com.sanq.product.config.utils.annotation.LogAnnotation;
import com.sanq.product.config.utils.entity.Pager;
import com.sanq.product.config.utils.entity.Pagination;
import com.sanq.product.config.utils.entity.Response;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/api/logs")
public class LogsController {

	@Resource
	private LogsService logsService;

	@GetMapping(value="/get/{id}")
	public Response getById(HttpServletRequest request, @PathVariable("id") Integer logId) {

		LogsVo logsVo = logsService.findById(logId);

		return logsVo != null ? new Response().success(logsVo) : new Response().failure();
	}

	@DeleteMapping(value="/delete")
	public Response deleteById(HttpServletRequest request, @RequestBody LogsVo logsVo) {

		int result = logsService.delete(logsVo);

		return result != 0 ? new Response().success() : new Response().failure();
	}

	@GetMapping(value="/list")
	public Response findListByPager(HttpServletRequest request, LogsVo logsVo, Pagination pagination) {

		Pager<LogsVo> pager = logsService.findListByPage(logsVo, pagination);

		return new Response().success(pager);
	}

	@GetMapping(value="/all")
	public Response findList(HttpServletRequest request, LogsVo logsVo) {

		List<LogsVo> list = logsService.findList(logsVo);

		return list != null ? new Response().success(list) : new Response().failure();
	}

	@PostMapping(value="/save")
	public Response add(HttpServletRequest request, @RequestBody LogsVo logsVo) {

		int result = logsService.save(logsVo);

		return result != 0 ? new Response().success() : new Response().failure();
	}

	@PutMapping(value = "/update/{id}")
	public Response updateByKey(HttpServletRequest request, @RequestBody LogsVo logsVo, @PathVariable("id") Integer logId) {

		int result = logsService.update(logsVo, logId);

		return result != 0 ? new Response().success() : new Response().failure();
	}
}